package com.stox.core.model;

import java.io.Serializable;

import com.stox.core.model.exception.StoxException;
import com.stox.core.model.intf.HasBars;
import com.stox.core.util.StringUtil;
import com.stox.resources.i18n.MessageSource;

public class Sector implements Serializable, Comparable<Sector>, HasBars {
	private static final long serialVersionUID = -6353872040362844289L;
	
	private String code;
	private String name;
	private String exchangeCode;

	public void validate(){
		if(!StringUtil.hasText(getCode())){
			throw new StoxException(MessageSource.getMessage("Sector.code.is.null.or.empty"));
		}
		if(!StringUtil.hasText(getName())){
			throw new StoxException(MessageSource.getMessage("Sector.name.is.null.or.empty"));
		}
		if(!StringUtil.hasText(getExchangeCode())){
			throw new StoxException(MessageSource.getMessage("Exchange.code.for.sector.is.null.or.empty"));
		}
	}
	
	public void format(){
		validate();
		setCode(getCode().trim().toUpperCase());
		setName(getName().trim());
		setExchangeCode(getExchangeCode().trim().toUpperCase());
	}
	
	@Override
	public int compareTo(Sector o) {
		if(o != null && StringUtil.hasText(o.name) && StringUtil.hasText(name)){
			return name.compareToIgnoreCase(o.name);
		}
		return 0;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getExchangeCode() {
		return exchangeCode;
	}

	public void setExchangeCode(String exchangeCode) {
		this.exchangeCode = exchangeCode;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((code == null) ? 0 : code.hashCode());
		result = prime * result
				+ ((exchangeCode == null) ? 0 : exchangeCode.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Sector other = (Sector) obj;
		if (code == null) {
			if (other.code != null)
				return false;
		} else if (!code.equals(other.code))
			return false;
		if (exchangeCode == null) {
			if (other.exchangeCode != null)
				return false;
		} else if (!exchangeCode.equals(other.exchangeCode))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return getName();
	}
	
}
